package com.itheima.dao.ipml;

import com.itheima.dao.BrardDao;
import com.itheima.dao.SingltonldbcTemplate;
import com.itheima.models.Brand;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Component("brandServce")
public class BrardDaolmpl implements BrardDao {


    @Override
    public List<Brand> find_all(){
        JdbcTemplate jdbcTemplate = SingltonldbcTemplate.getJdbcTemplate();
        String sql = "selsct * from brands";
        List<Brand> brands = jdbcTemplate.query(sql, new RowMapper<Brand>() {
            @Override
            public Brand mapRow(ResultSet resultSet, int i) throws SQLException {
                Brand brand = new Brand();
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String tag = resultSet.getString("tag");
                brand.setId(id);
                brand.setName(name);
                brand.setTag(tag);
                return brand;
            }

        });
        return brands;
    }

    @Override
    public int caeate(Brand brand) {
        JdbcTemplate jdbcTemplate = SingltonldbcTemplate.getJdbcTemplate();
        String sql ="INSERT INTO brands(NAME, tag) VALUE(\"adi\",\"sports\");";
        jdbcTemplate.execute(sql);
        return 0;
    }

    @Override
    public int update(Brand brand) {
        return 0;
    }

    @Override
    public int delete(int id) {
        return 0;
    }
}
